package cn.xspace.app.filemg.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.xspace.android.xafcommon.util.BaseRProxy;

import java.util.ArrayList;
import java.util.List;

import cn.xspace.app.filemg.bean.FileInfo;
import cn.xspace.app.filemg.util.FileMg;

@SuppressLint("NewApi") 
public class FileMgAdapter extends BaseAdapter {
	private Context context;
//	private DisplayImageOptions options;
	List<FileInfo> fileList;
	public FileMgAdapter(Context context, ArrayList<FileInfo> objects) {
//		super(context, 0, objects);
		this.context = context;
		this.fileList=objects;
//		options = new DisplayImageOptions.Builder().showImageForEmptyUri(RProxy.picturndefault1).showImageOnFail(RProxy.picturndefault2).resetViewBeforeLoading().cacheInMemory()
//				.cacheOnDisc().imageScaleType(ImageScaleType.EXACTLY).bitmapConfig(Bitmap.Config.RGB_565).displayer(new FadeInBitmapDisplayer(300)).build();
	
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			convertView = inflater.inflate(BaseRProxy.folder_grid_item, null);
			viewHolder = new ViewHolder(convertView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		FileInfo info = (FileInfo) getItem(position);
		viewHolder.image.setBackgroundResource(0);
		if (info.isFolder()) {
			viewHolder.mName.setVisibility(View.GONE);
			viewHolder.mCheckBox.setVisibility(View.GONE);
			viewHolder.appSize.setVisibility(View.GONE);
			viewHolder.appTime.setVisibility(View.GONE);
			viewHolder.folder_name.setVisibility(View.VISIBLE);
			viewHolder.ItemText_next.setVisibility(View.VISIBLE);
			viewHolder.folder_name.setText(info.getName());
			viewHolder.image.setBackgroundResource(BaseRProxy.folder);

		} else {
			viewHolder.folder_name.setVisibility(View.GONE);
			viewHolder.ItemText_next.setVisibility(View.GONE);
			viewHolder.mName.setVisibility(View.VISIBLE);
			viewHolder.mCheckBox.setVisibility(View.VISIBLE);
			viewHolder.appSize.setVisibility(View.VISIBLE);
			viewHolder.appTime.setVisibility(View.VISIBLE);
			viewHolder.mName.setText(info.getName());
			viewHolder.appSize.setText(FileMg.FormetFileSize(info.getSize()));
			viewHolder.appTime.setText(info.getTime());
			viewHolder.mCheckBox.setChecked(info.isCheckFlag());
			if ("mp3".equals(info.getFileType())
					|| "wma".equals(info.getFileType())) {
				viewHolder.image.setBackgroundResource(BaseRProxy.file_icon_mp3);
			} else if ("mp4".equals(info.getFileType())
					|| "rmvb".equals(info.getFileType())) {
				viewHolder.image.setBackgroundResource(BaseRProxy.file_icon_video);
			} else if ("doc".equals(info.getFileType())
					|| "docx".equals(info.getFileType())
					|| "wps".equals(info.getFileType())
					|| "xls".equals(info.getFileType())) {
				viewHolder.image.setBackgroundResource(BaseRProxy.file_icon_office);
			} else if ("pdf".equals(info.getFileType())) {
				viewHolder.image.setBackgroundResource(BaseRProxy.file_icon_pdf);
			} else if ("jpg".equals(info.getFileType())
					|| "png".equals(info.getFileType())) {
//				ImageUtils.imageLoader.displayImage(StringUtils.getUri(info.getPath()), viewHolder.image, options);
				if (info.getApkIoc() == null || "".equals(info.getApkIoc())) {
					info.setApkIoc(FileMg.getImageThumbnail(info.getPath()));
					viewHolder.image.setBackground(info.getApkIoc());
				} else {
					viewHolder.image.setBackground(info.getApkIoc());
				}
			} else if ("jar".equals(info.getFileType())
					|| "rar".equals(info.getFileType())) {
				viewHolder.image.setBackgroundResource(BaseRProxy.file_icon_rar);
			} else if ("zip".equals(info.getFileType())) {
				viewHolder.image.setBackgroundResource(BaseRProxy.file_icon_zip);
			} else if ("tex".equals(info.getFileType())
					|| "ebk3".equals(info.getFileType())) {
				viewHolder.image.setBackgroundResource(BaseRProxy.file_icon_txt);
			} else {
				viewHolder.image
						.setBackgroundResource(BaseRProxy.file_icon_default);
			}
		}

		return convertView;
	}

	class ViewHolder {
		public ImageView image;
		public TextView folder_name;
		public ImageView ItemText_next;
		public TextView mName;
		public CheckBox mCheckBox;
		public TextView appSize;
		public TextView appTime;
		public ViewHolder(View v) {
			mCheckBox = (CheckBox) v.findViewById(BaseRProxy.box);
			image = (ImageView) v.findViewById(BaseRProxy.ItemImage);
			mName = (TextView) v.findViewById(BaseRProxy.ItemText);
			ItemText_next = (ImageView) v.findViewById(BaseRProxy.ItemText_next);
			folder_name = (TextView) v.findViewById(BaseRProxy.folder_name);
			appSize = (TextView) v.findViewById(BaseRProxy.ItemText_size);
			appTime = (TextView) v.findViewById(BaseRProxy.ItemText_time);
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return fileList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return fileList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
}
